//
//  HomeViewController.m
//  AloTruyen
//
//  Created by minhthong on 9/10/14.
//  Copyright (c) 2014 Khoai Nguyen Minh. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCell.h"
#import "ListBookViewController.h"
#import "AloTruyenModal.h"
@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UISearchBarDelegate>
@property (nonatomic,strong) NSMutableArray *arrTitle;

@property (nonatomic,weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong)  UISearchBar *searchBar;
@end

@implementation HomeViewController
#pragma mark - Initalize
-(NSMutableArray *)arrTitle{
    if (!_arrTitle) {
        _arrTitle = [NSMutableArray array];
    }
    return _arrTitle;
}
#pragma mark - Customize Subview
-(void)customizeBar{
    @autoreleasepool {
        self.leftButtonImageName = @"list";
    }
}
-(void)customizeSubView{
    

    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView reloadData];
    
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 210, 44)];
    searchBar.delegate = self;
    self.navigationItem.titleView = searchBar;
}

#pragma mark - Button Action
-(void)didTouchedOnLeftButton:(id)sender{
    [self.navigationController.revealController showViewController:self.navigationController.revealController.leftViewController animated:YES completion:^(BOOL finished) {
        
    }];
}
-(void)didTouchedOnRightButton:(id)sender{
    [self.navigationController.revealController showViewController:self.navigationController.revealController.rightViewController animated:YES completion:^(BOOL finished) {
        
    }];
}
#pragma mark - NSNotification
-(void)handleLeftMenuWithSpecID:(NSNotification*)notifi{
    LeftMenu *menu = notifi.object;
    NSString *specID = menu.specID;
    [appDelegate.dataCenter getBookFromLeftMenuWithSpecID:specID
                                                 andBlock:^(id result, id error) {
                                                     if (result) {
                                                         [self.arrTitle removeAllObjects];
                                                         [self.arrTitle addObjectsFromArray:result];
                                                         [self.collectionView reloadData];
                                                     }else{
                                                         [NLUtils showAlertWithMessage:error];
                                                     }
                                                 }];
    NSLog(@"Spec ID : %@",specID);
}
#pragma mark - Init life vehicle

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self customizeBar];
    
    [super viewDidLoad];
    [self customizeSubView];
    NotifReg(self, @selector(handleLeftMenuWithSpecID:), @"kLeftMenu");
    
    [appDelegate.dataCenter getMenuFromHomeWithBlock:^(id result, id error) {
        [self.arrTitle addObjectsFromArray:result];
        [self.collectionView reloadData];
    }];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    NotifUnregAll(self);
}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    if (SupportiOS7) {
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
            self.edgesForExtendedLayout = UIRectEdgeNone;
        
        CGRect viewBounds = self.view.bounds;
        CGFloat topBarOffset = self.topLayoutGuide.length;
        viewBounds.origin.y = topBarOffset * -1;
        
        self.view.bounds = viewBounds;
        self.navigationController.navigationBar.translucent = NO;
    }
}
#pragma mark - UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return self.arrTitle.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    @autoreleasepool {
        HomeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"HomeCell" forIndexPath:indexPath];
        AloTruyenModal *params = self.arrTitle[indexPath.row];
        /* image cell */
        cell.icon.layer.masksToBounds = YES;
        cell.icon.layer.cornerRadius = cell.icon.frame.size.height / 2;
        cell.icon.layer.borderWidth = 3;
        cell.icon.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [cell.icon setImageWithURL:params.thumbNail placeholderImage:[NLUtils imageFromColor:UIColorFromRGB(0x256213)]];
        cell.title.text = params.titleBook;
    
//        cell.icon.image = [NLUtils imageFromColor:UIColorFromRGB(0x256213)];
        /* title */
//        cell.title.text = params[[[self class] description]];
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(105, 110);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
